/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.core.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.core.dao.CityDAO;
import com.core.database.Manager;
import com.core.entities.City;
import com.core.exception.DAOException;

/**
 * 
 * @author himanasser
 */
public class CityDAOImpl implements CityDAO {
	
	ArrayList<City> cityList = new ArrayList<City>();
	City city = new City();


	Connection connection = null;
	private static CityDAOImpl instance=null;
	public static CityDAOImpl getInstance(){
		if(instance==null){
			instance=new CityDAOImpl();
		}
		return instance;
	}


	@Override
	public City findByName(String name) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "SELECT * FROM city WHERE name = '" + name + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
				if (count == 1) {
					city.setId(rs.getInt(1));
					city.setName(rs.getString(2));
					city.setState_id(rs.getInt(3));
				}
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
		return city;
	}

	@Override
	public List<City> findByState_id(int state_id) throws DAOException {

		
		try {
			connection = Manager.getConnection();
			String sql = "SELECT * FROM city WHERE state_id = '" + state_id
					+ "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				City city = new City();
				city.setId(rs.getInt(1));
				city.setName(rs.getString(2));
				city.setState_id(rs.getInt(3));
				cityList.add(city);
			}
		} catch (SQLException s) {
			s.printStackTrace();
		}
		return cityList;
	}

	@Override
	public void insert(City o) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "INSERT INTO city (name, state_id) VALUES (?, ?)";
			PreparedStatement ps = connection.prepareStatement(sql);
			ps.setString(1, o.getName());
			ps.setInt(2, o.getState_id());
			int result = ps.executeUpdate();
			if (result != 1) {
				throw new SQLException("no row inserted!");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new RuntimeException("Cannot add new City", ex);
		} finally {
			Manager.closeConnection(connection);
		}
	}

	@Override
	public City findById(int id) throws DAOException {
		City city = new City();
		try {
			connection = Manager.getConnection();
			String sql = "SELECT * FROM city WHERE id = '" + id + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
				if (count == 1) {
					city.setId(rs.getInt(1));
					city.setName(rs.getString(2));
					city.setState_id(rs.getInt(3));

				}
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
		return city;

	}

	@Override
	public List<City> findAll() throws DAOException {
		City city = new City();
		ArrayList<City> cityList = new ArrayList<City>();
		try {
			connection = Manager.getConnection();
			String sql = " SELECT * FROM city ";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				city.setId(rs.getInt(1));
				city.setName(rs.getString(2));
				city.setState_id(rs.getInt(3));
				cityList.add(city);

			}
		} catch (SQLException s) {
			s.printStackTrace();
		}
		return cityList;
	}

	@Override
	public void delete(int id) throws DAOException {
		City city = new City();
		try {
			connection = Manager.getConnection();
			String sql = " DELETE FROM city WHERE id = '" + id + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			int result = ps.executeUpdate();
			if (result != 1) {
				ps.setString(1, city.getName());
				ps.setInt(2, city.getState_id());
			}

		} catch (SQLException s) {
			s.printStackTrace();

		}
	}

	@Override
	public void update(City o) throws DAOException {

		try {
			connection = Manager.getConnection();
			String sql = "UPDATE city SET name = '" + o.getName()
					+ " ', state_id = '" + o.getState_id() + "' WHERE id = "
					+ o.getId();
			PreparedStatement ps = connection.prepareStatement(sql);
			int result = ps.executeUpdate(sql);

			if (result != 1) {
				ps.setString(1, o.getName());
				ps.setInt(2, o.getState_id());
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
	}
}
